<?php

namespace App\Listeners;

use App\Event\AdministratorLogsEvent;
use App\Models\Record as RecordModel;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

class AdministratorLogsListener
{
    public $request = null;
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * Handle the event.
     *
     * @param  \App\Event\AdministratorLogsEvent  $event
     * @return void
     */
    public function handle(AdministratorLogsEvent $event)
    {
        $desc = $event->desc;
        $account = $event->account;
        if (empty($account)) $account = $this->request->session()->get('account');
        $model = new RecordModel();
        $model->log_account = $account;
        $model->log_menu = $this->request->path();
        $model->log_method = $this->request->method();
        $model->log_params = Arr::query($this->request->query());
        $model->log_desc = $desc;
        $model->log_ip = $this->request->ip();
        $model->save();
    }
}
